import { PageContainer } from '@ant-design/pro-layout';
import { Table,Card,Button,Modal, Input,Popover } from 'antd';
import {SearchOutlined,PlusOutlined} from '@ant-design/icons'
import {UseSearch,UseTable} from '@/components'
import React from 'react';
import moment from 'moment';
import OfferedController from '@/services/OfferedProject/OfferedController';
import { getStr } from '@/components/function';
import Detail from '../detail'
import CompleteRecord from './CompleteRecord'
import NewProjectOne from './NewProjectOne';
const {confirm} = Modal
class ProjectList extends React.Component {
    constructor(props) {
        super(props);
        this.state = { 
            dataSource:[],
            freshen:false,
            userInfo:JSON.parse(sessionStorage.getItem("userInfo"))
         };
    }
    showNewProject=()=>{
        this.setState({
            visibleNewProject:true
        })
    }
    freshen=()=>{
        this.setState({freshen:!this.state.freshen})
    }
    handleSearch=(query)=>{
        this.setState({query})
        this.freshen()

    }
    reset=()=>{
        this.setState({query:{}})
        this.freshen()

    }
    getList=(query)=>{
        return OfferedController.getProjectList(query)
    }
    onCancel=()=>{
        this.setState({visibleNewProject:false,visibleResult:false})
        this.freshen()
    }
    toDetail=(rowData)=>{
        this.setState({
            rowData,
            visibleDetail:true
        })
    }
    toResult=(rowData)=>{
        this.setState({
            rowData,
            visibleResult:true
        })
    }
    onRef=(ref)=>{
        this.setState({
            modal:ref
        })
    }
    toStop=(rowData)=>{
        let that = this
        confirm({
            title:'提示',
            content:'确定要终止该任务吗？',
            onOk(){
                that.freshen()
            }
        })
    }
    onRef=(ref)=>{
        this.setState({modal:ref})
    }
    submit=()=>{
        this.state.modal.submit()
    }
    toFiles=(text)=>{
        this.setState({
            visibleFile:true,
            dataSource:JSON.parse(text)
        })
    }
    toCompleteRecord=(rowData)=>{
        this.setState({
            visibleComplete:true,
            rowData
        })
    }
    render() {
        const {rowData,visibleNewProject,freshen,visibleDetail,query,dataSource,visibleFile,visibleComplete}=this.state
        const columns=[

            {title:'项目名称',dataIndex:'projectName',align:'center'},
            {title:'所属部门',dataIndex:'department',align:'center',render:text=>text&&text.split("/")[1]},
            {title:'任务描述',dataIndex:'description',align:'center',render:text=>getStr(text)},
            {title:'发布人',dataIndex:'createdBy',align:'center'},
            {title:'发布时间',dataIndex:'createdAt',align:'center'},
            {title:'相关附件',dataIndex:'fileItems',align:'center',render:text=>text&&<a onClick={this.toFiles.bind(this,text)}>查看</a>},
            {title:'计划认领人数',dataIndex:'participantNumber',align:'center'},
            {title:'级别',dataIndex:'level',align:'center'},
            {title:'审核结果',dataIndex:'applyReviewResult',align:'center'},
            {title:'操作',align:'center',render:(text,rowData)=><div>
                <Button onClick={this.toStop.bind(this,rowData)} type={'primary'} style={{margin:5}}>终止</Button>
                <Button onClick={this.toCompleteRecord.bind(this,rowData)} type={'primary'} style={{margin:5}}>完成情况</Button>
                </div>},
        ]
        const buttonStyle={margin:5}
        const searchItem=[
            // {name:'项目审核结果',keyName:'leaderUserName',valueType:'text'},
            {name:'项目名称',keyName:'projectName',valueType:'text'},
        ]
        const fileColumns=[
            {title:'序号',dataIndex:'url',render:(text,row,index)=>index+1},
            {title:'文件名',dataIndex:'fileName'},
            {title:'描述',dataIndex:'description',render:text=>getStr(text)},
            {title:'操作',dataIndex:'url',render:(text,row,index)=><a href={text} download={row.fileName}>下载</a>},
        ]
        return (
            <PageContainer
            extra={<Button onClick={this.showNewProject} type={'primary'} icon={<PlusOutlined/>} style={buttonStyle}>发布任务</Button>}
            >
                <Card>
                <UseSearch searchItem={searchItem} handleSearch={this.handleSearch} reset={this.reset}/>  
                </Card>
                <Card>
                    <UseTable columns={columns} getData={this.getList} bordered rowKey={'projectCode'} refresh={freshen} query={query}/>
                </Card>
                <Modal
                visible={visibleNewProject}
                title={'发布悬赏项目'}
                width={'1200px'}
                maskClosable={false}
                onCancel={()=>this.setState({visibleNewProject:false})}
                onOk={this.submit}
                >
                    {visibleNewProject&&<NewProjectOne onCancel={this.onCancel} onRef={this.onRef}/>}
                </Modal>
                <Modal
                visible={visibleDetail}
                title={'项目详情'}
                onCancel={()=>this.setState({visibleDetail:false})}
                footer={false}
                width={"80%"}
                >
                    {visibleDetail&&<Detail rowData={rowData}/>}
                </Modal>
                {visibleFile&&<Modal
                visible={visibleFile}
                onCancel={()=>this.setState({visibleFile:false})}
                footer={false}
                >
                    <Table dataSource={dataSource} columns={fileColumns} bordered rowKey={'url'} />
                </Modal>}
                <Modal
                visible={visibleComplete}
                title="查看完成情况"
                onCancel={()=>this.setState({visibleComplete:false})}
                width={'80%'}
                footer={false}
                >
                    {visibleComplete&&<CompleteRecord rowData={rowData} />}
                </Modal>
            </PageContainer>
        );
    }
}

export default ProjectList;